package com.pro.springbootpro.controller;

import com.github.pagehelper.PageInfo;
import com.pro.springbootpro.entity.People;
import com.pro.springbootpro.mapper.PeopleMapper;
import com.pro.springbootpro.service.IPeopleService;
import com.pro.springbootpro.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author DJR (1210843430@qq.com)
 */

@Slf4j
@RestController
@RequestMapping("/first")
public class FirstController {
    ReentrantReadWriteLock.ReadLock r = new ReentrantReadWriteLock(true).readLock();

    @Autowired
    private RedisUtil redisUtil;

    public static final String LOCK_NAME = "name";

    private int count = 20;

    /**
     * @return
     */
    @PreAuthorize(value = "hasRole('ADMIN')")
    @RequestMapping(value = "/getOne", method = RequestMethod.POST)
    public String getFirst() {
        log.info("dasdasdadasda");
        return "admin角色";
    }

    /**
     * @return
     */
    @PreAuthorize(value = "hasRole('USER')")
    @RequestMapping(value = "/getTwo", method = RequestMethod.POST)
    public String getTwo() {
        log.info("getTwo");
        return "USER角色";
    }

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public Object get() {
        redisUtil.set("djr", "djr");
        List<People> peopleList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            People p = new People();
            p.setAge(i + 18);
            p.setId(i + 1);
            p.setName("djr" + i);
            p.setSex(i % 2);
            peopleList.add(p);
        }
        redisUtil.set("allPeople", peopleList);
        return redisUtil.get("allPeople");
    }

    @Autowired
    private PeopleMapper peopleMapper;

    @GetMapping("/getById/{id}")
    public People getById(@PathVariable(value = "id") int id) {
        return peopleMapper.getById(id);
    }

    @Autowired
    private IPeopleService peopleService;

    @PostMapping("/getByPage")
    public PageInfo<People> getById(@RequestBody Map<String, Object> paramMap) {
        Integer pageNum = Integer.parseInt(paramMap.get("pageNum").toString());
        Integer pageSize = Integer.parseInt(paramMap.get("pageSize").toString());
//        return peopleMapper.getByPage((pageNum-1)*pageSize,pageSize);
        return peopleService.getByPage(pageNum, pageSize);
    }

    @PostMapping("/save")
    public String save(@RequestBody Map<String, Object> paramMap) {
        List<People> peopleList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            People p = new People();
            p.setName("武魂殿" + i);
            p.setAge(i);
            p.setSex(i % 2 == 0 ? 0 : 1);
            peopleList.add(p);
        }
        peopleService.save(peopleList);
        return "success";
    }
}
